﻿using System;
using System.Windows;
using Bricks;
using Bricks.Injecting;

namespace Motar.Desktop
{

    /// <summary>
    /// 
    /// </summary>
    public class DialogService : FacadeBase<IContainerFacade>, IDialogService
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="containerFacade"></param>
        public DialogService(IContainerFacade containerFacade) :
            base(() => containerFacade)
        {
            if (containerFacade == null)
            {
                throw new ArgumentNullException("containerFacade");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public MessageBoxResult Show(string content)
        {
            return MessageBox.Show(content);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public MessageBoxResult Show(Window owner, string content)
        {
            return MessageBox.Show(owner, content);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        /// <param name="caption"></param>
        /// <returns></returns>
        public MessageBoxResult Show(string content, string caption)
        {
            return MessageBox.Show(content, caption);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="content"></param>
        /// <param name="caption"></param>
        /// <returns></returns>
        public MessageBoxResult Show(Window owner, string content, string caption)
        {
            return MessageBox.Show(owner, content, caption);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        /// <param name="caption"></param>
        /// <param name="button"></param>
        /// <returns></returns>
        public MessageBoxResult Show(string content, string caption, MessageBoxButton button)
        {
            return MessageBox.Show(content, caption, button);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="content"></param>
        /// <param name="caption"></param>
        /// <param name="button"></param>
        /// <returns></returns>
        public MessageBoxResult Show(Window owner, string content, string caption, MessageBoxButton button)
        {
            return MessageBox.Show(owner, content, caption, button);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        /// <param name="caption"></param>
        /// <param name="button"></param>
        /// <param name="icon"></param>
        /// <returns></returns>
        public MessageBoxResult Show(string content, string caption, MessageBoxButton button, MessageBoxImage icon)
        {
            return MessageBox.Show(content, caption, button, icon);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="content"></param>
        /// <param name="caption"></param>
        /// <param name="button"></param>
        /// <param name="icon"></param>
        /// <returns></returns>
        public MessageBoxResult Show(Window owner, string content, string caption, MessageBoxButton button, MessageBoxImage icon)
        {
            return MessageBox.Show(owner, content, caption, button, icon);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        /// <param name="caption"></param>
        /// <param name="button"></param>
        /// <param name="icon"></param>
        /// <param name="defaultResult"></param>
        /// <returns></returns>
        public MessageBoxResult Show(string content, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult)
        {
            return MessageBox.Show(content, caption, button, icon, defaultResult);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="content"></param>
        /// <param name="caption"></param>
        /// <param name="button"></param>
        /// <param name="icon"></param>
        /// <param name="defaultResult"></param>
        /// <returns></returns>
        public MessageBoxResult Show(Window owner, string content, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult)
        {
            return MessageBox.Show(owner, content, caption, button, icon, defaultResult);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        /// <param name="caption"></param>
        /// <param name="button"></param>
        /// <param name="icon"></param>
        /// <param name="defaultResult"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public MessageBoxResult Show(string content, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, MessageBoxOptions options)
        {
            return MessageBox.Show(content, caption, button, icon, defaultResult, options);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="content"></param>
        /// <param name="caption"></param>
        /// <param name="button"></param>
        /// <param name="icon"></param>
        /// <param name="defaultResult"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public MessageBoxResult Show(Window owner, string content, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, MessageBoxOptions options)
        {
            return MessageBox.Show(owner, content, caption, button, icon, defaultResult, options);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TDialog"></typeparam>
        public void Show<TDialog>() where TDialog : Window
        {
            Show<TDialog>(Application.Current.MainWindow, nr => { });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TDialog"></typeparam>
        /// <param name="owner"></param>
        public void Show<TDialog>(Window owner) where TDialog : Window
        {
            Show<TDialog>(owner, nr => { });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TDialog"></typeparam>
        /// <param name="initialization"></param>
        public void Show<TDialog>(Action<TDialog> initialization) where TDialog : Window
        {
            Show<TDialog>(Application.Current.MainWindow, initialization);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TDialog"></typeparam>
        /// <param name="owner"></param>
        /// <param name="initialization"></param>
        public void Show<TDialog>(Window owner, Action<TDialog> initialization) where TDialog : Window
        {
            if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }
            if (initialization == null)
            {
                throw new ArgumentNullException("initialization");
            }

            Execute(containerFacade =>
            {
                TDialog dialog = null;

                if (containerFacade.TryResolve<TDialog>(out dialog))
                {
                    initialization.Invoke(dialog);

                    dialog.Owner = owner;

                    dialog.ShowInTaskbar = false;

                    dialog.ShowDialog();
                }
            });
        }
    }
}
